package co.com.javeriana.aes.oms.ejb.util.ldap.connection;

import java.util.Hashtable;
import java.util.Random;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.naming.Context;
import javax.naming.NamingException;
import javax.naming.directory.Attribute;
import javax.naming.directory.Attributes;
import javax.naming.directory.DirContext;
import javax.naming.directory.InitialDirContext;

/**
 *
 * @author Jonathan Fernandez <jonathan.fvargas@gmail.com>
 */
public class Connection {

    private static final String USER_NAME = "jonathan";
    private static final String PASSWORD = "jfernandez123";
    private static final String DN = "cn=jonathan,ou=Users,dc=oms,dc=com";

    private static final String BASE = "dc=oms,dc=com";
    private static final String LDAP_URL = "ldap://192.168.1.100:389";
    private static final String CONTEXT_FACTORY = "com.sun.jndi.ldap.LdapCtxFactory";
    private static final String SECURITY_AUTHENTICATION = "simple";
    private static final String DEPTO_ASSOCIATED = "departmentNumber";

    //dummy test
    private static Random rnd = new Random();

    /**
     * @param args the command line arguments
     */
    public static void main(String[] args) {

        System.out.println(validateUser(USER_NAME, PASSWORD));

    }

    public static boolean getRandomBoolean() {
        return rnd.nextBoolean();
    }

    public static boolean validateUser(String userName, String password) {

        DirContext context = null;
        boolean validate = false;

        try {
            context = getDirContext(userName, password);
            if (context != null) {
                validate = true;
            }

        } finally {

            if (context != null) {
                try {
                    context.close();
                } catch (NamingException ex) {
                    Logger.getLogger(Connection.class.getName()).log(Level.SEVERE, null, ex);
                }
            }
        }

        return validate;
    }

    public static String getUserPermission(String userName, String password) {

        String permission = null;
        DirContext authContext = null;

        try {
            authContext = getDirContext(userName, password);
            // Get all the attributes of named object            
            Attributes answer = authContext.getAttributes("cn="+userName+",ou=Users,dc=oms,dc=com");
            Attribute permissionAttribute = answer.get(DEPTO_ASSOCIATED);
            permission = String.valueOf(permissionAttribute.get());

        } catch (javax.naming.AuthenticationException e) {
            Logger.getLogger(Connection.class.getName()).log(Level.SEVERE, null, e);
        } catch (NamingException e) {
            Logger.getLogger(Connection.class.getName()).log(Level.SEVERE, null, e);
        } finally {
            try {
                if (authContext != null) {
                    authContext.close();
                }
            } catch (NamingException ex) {
                Logger.getLogger(Connection.class.getName()).log(Level.SEVERE, null, ex);
            }
        }
        return permission;
    }

    private static DirContext getDirContext(String userName, String password) {

        Hashtable<String, String> authEnv = new Hashtable<String, String>();
        DirContext authContext = null;

        authEnv.put(Context.INITIAL_CONTEXT_FACTORY, CONTEXT_FACTORY);
        authEnv.put(Context.PROVIDER_URL, LDAP_URL);
        authEnv.put(Context.SECURITY_AUTHENTICATION, SECURITY_AUTHENTICATION);

        String searchDN = "cn=" + userName + "," + "ou=Users" + "," + BASE;
        authEnv.put(Context.SECURITY_PRINCIPAL, searchDN);
        authEnv.put(Context.SECURITY_CREDENTIALS, password);

        try {
            authContext = new InitialDirContext(authEnv);
        } catch (javax.naming.AuthenticationException e) {
            Logger.getLogger(Connection.class.getName()).log(Level.SEVERE, null, e);
        } catch (NamingException e) {
            Logger.getLogger(Connection.class.getName()).log(Level.SEVERE, null, e);
        }
        return authContext;
    }

}
